package com.example.demo.controller;

import com.example.demo.dao.BookDao;
import com.example.demo.domain.Book;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "用户controller", tags = {"用户操作接口"})
@RestController
public class DemoController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private BookDao bookDao;

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ApiOperation(value = "测试", notes = "测试")
    public Book testmode() {
        Book book = new Book("码农翻身", "100.0", "张三");
        //ValueOperations<String, Book> operations = redisTemplate.opsForValue();
        redisTemplate.opsForValue().set("fdd2", book.toString());
        //operations.set("fdd2", book);
        boolean exists = redisTemplate.hasKey("fdd2");
        System.out.printf("redis是否存在相应的key:" + exists);
        //Book newBook = (Book) redisTemplate.opsForValue().get("fdd2");
        String newBook = (String) redisTemplate.opsForValue().get("fdd2");
        System.out.printf("从redis数据库获取的book：" + newBook.toString());
        return book;
    }

    @RequestMapping(value = "/test2",method = RequestMethod.GET)
    @ApiOperation(value = "缓存测试")
    public void contextLoads(){
        bookDao.getBookById(100);
        String book = bookDao.getBookById(100);
        System.out.printf(book);
        System.out.printf("\r\n");

//        String book1 = bookDao.updateBookById(100);
//        System.out.printf(book1);
//        System.out.printf("\r\n");
//
//        String book2 = bookDao.getBookById(100);
//        System.out.printf(book2);
//        System.out.printf("\r\n");
//
//        bookDao.deleteById(100);
//        System.out.printf("\r\n");
//
//        bookDao.getBookById(100);
//        System.out.printf("\r\n");
//
//        bookDao.getBookById2(99);
//        bookDao.getBookById2(99);
//        System.out.printf("\r\n");
    }
}
